package com.WaVel.service.impl;

import com.Emplyment.dto.CategoryDto;
import com.Emplyment.entity.EmplymentCategory;
import com.Emplyment.exception.BaseException;
import com.Emplyment.exception.RegisterException;
import com.WaVel.mapper.ArticleMapper;
import com.WaVel.mapper.CategoryMapper;
import com.WaVel.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public List<EmplymentCategory> SearchEmplymentCategoryAll() {
        List<EmplymentCategory> categoryList = categoryMapper.SearchAllEmplymentCategory();
        return categoryList;
    }

    /**
     * 添加公司分类
     * @param categoryDto
     */
    @Override
    public void addEmplymentCategory(CategoryDto categoryDto) {
        if (categoryDto.getCategoryName().equals("") || categoryDto.getCategoryAlias().equals("")){
            throw new RegisterException("公司分类添加错误");
        }

        EmplymentCategory Category = categoryMapper.SearchEmplymentCategory(categoryDto);
        if (Category != null){
            throw new RegisterException("此公司信息已存在");
        }
        
        EmplymentCategory emplymentCategory = new EmplymentCategory();
        BeanUtils.copyProperties(categoryDto,emplymentCategory);
        categoryMapper.addEmplymentCategory(emplymentCategory);
    }


    /**
     * 修改分类
     * @param dto
     */
    @Override
    public void UpdataEmplymentCategory(CategoryDto dto) {
        if (dto.getCategoryName().equals("") || dto.getCategoryAlias().equals("")){
            throw new RegisterException("公司分类修改错误");
        }

        EmplymentCategory Category = categoryMapper.SearchEmplymentCategory(dto);
        if (Category != null){
            throw new RegisterException("此公司信息已存在");
        }

        EmplymentCategory emplymentCategory = new EmplymentCategory();
        BeanUtils.copyProperties(dto,emplymentCategory);
        categoryMapper.UpdataEmplymentCategory(emplymentCategory);

    }



    /**
     * 根据id删除公司分类
     * @param id
     */
    @Override
    public void DelectEmplymentCategory(int id) {
        List<Integer> categoryIds = articleMapper.getCategoryIds();
        if (categoryIds.contains(id)){
            throw  new BaseException("此分类下存在数据，不能删除");
        }
        categoryMapper.DelectEmplymentCategory(id);
    }
}
